import {CreatePunch, GetSubCategory, GetTravels, GetUserTravelsDetail} from "../../model/travels";

Page({
    data: {
        image: {
            activeImage: 'https://kt-public.tempot.com/poems/covers/detail-tab-icon.png',
            defaultImage: '',
        },
        arr: [],
        category: '',
        punchList: []
    },
    category: '',
    onLoad: function (options) {
        this.category = options.category
        wx.setNavigationBarTitle({
            title: this.category
        })
        this.setData({
            category: this.category
        })
        GetSubCategory({
            category: this.category
        }).then(res => {
            console.log(res)
            this.setData({
                arr: res.data
            })
            this.getListWithSubKey(res.data[0])
        })
    },
    onShow() {
        GetUserTravelsDetail().then(res => {
            this.setData({
                punchList: res
            })
        })
    },
    getListWithSubKey(sub_key) {
        GetTravels({
            category: this.category,
            sub_category: sub_key
        }).then(res => {
            this.setData({
                locationList: res.data
            })

        })
    },
    changeTabs(e) {
        this.getListWithSubKey(e.detail.activeKey)
    },
    onDetail(e) {
        let item = e.currentTarget.dataset.item
        // wx.navigateTo({
        //     url: `/pages/travelDetail/index?id=${item.id}&title=${item.title}`
        // })
        if (this.data.punchList.indexOf(item.id) > -1) {
            wx.navigateTo({
                url: `/pages/travelDetail/index?id=${item.id}&title=${item.title}`
            })
        }else {
            CreatePunch(item.id, item.category).then(res => {
                if (res) {
                    wx.navigateTo({
                        url: `/pages/travelDetail/index?id=${item.id}&title=${item.title}`
                    })
                }
            }).catch(_ => {})
        }
    },
});
